package com.class04.json;

import com.class04.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassDescription:
 *
 * @JdkVersion: 17
 * @Author: yxy
 * @Created: 2023/12/7 11:31
 */
@RequestMapping("json")
//@ResponseBody  返回json的注解，添加到类和方法上
@RestController//==@ResponseBody+@Controller
public class JsonController {

    /**
     * @ResponseBody 数据直接放入响应体返回！ 也不会走视图解析器
     *      加了@ResponseBody后快速查找视图，转发和重定向都不生效了！
     * @return
     */
    @GetMapping("data")
    public User data(){
        //对象-》 json->{}
        //集合-》 json->[]
        return new User("zhangsan",18);
        //user -> handlerAdapter -> json ->
//        @ResponseBody  -> json直接返回 （前后端分离模式）
    }

    @GetMapping("data1")
    public List<Object> data1(){
        List<User> list = new ArrayList<>();
        list.add(new User("zhangsan",12));
        list.add(new User("lisi",40));
        list.add(new User("wangwu",6));
        list.add(new User("zhaoliu",50));
        list.add(new User("qianqi",10));
        List<User> collect = list.stream().sorted(Comparator.
                comparing(User::getAge)).collect(Collectors.toList());
        System.out.println("collect = " + collect);
//        根据年龄排序取前三条
        List<User> collect1 = list.stream().sorted(Comparator.
                comparing(User::getAge)).limit(3).collect(Collectors.toList());
        //        获取所有员工姓名
        List<String> collect2 = list.stream().map(User::getName).collect(Collectors.toList());
        return Collections.singletonList(collect);
    }

}
